<?php
namespace app\common\service;

use think\facade\Db;

class Pay extends Base {

    public function ready(){
        $ids = input('ids');
        $table = input('payt')?:'order';
        switch($table) {
            case 'recharge_log':
                $total_price = Db::name('recharge_log')->where(['id'=>['in',$ids]])->sum('money');
                $to_url = '/pages/my/usercenter';
                break;
            case 'videospider_order':
                $total_price = Db::name('videospider_order')->where(['id'=>['in',$ids]])->sum('pay_money');
                $to_url = '/pagesExt/videospider/orderlist';
                break;
            case 'seckill_order':
                $total_price = Db::name('seckill_order')->where(['id'=>['in',$ids]])->sum('pay_price');
                $to_url = '/pages/seckill/orderlist';
                break;
            default:
                $total_price = Db::name('order')->where(['order_id'=>['in',$ids]])->sum('total_shop_price');
                $to_url = '/pages/myorder/orderlist';
        }
        //获取支付规则
        $rs = logic('Pay')->getHtml($table,$ids,$this->user_id);
        if($rs['status']==0){
            return [
                'status' => 0,
                'msg'=>'获取支付规则失败，请联系管理员'
            ];
        }
        $rs['result']['total_price'] = $total_price;
       return [
           'status' => true,
           'data' => $rs['result'],
           'to_url' => $to_url
       ];
    }
    public function toPay(){

        $ids = input('id');
        $table = input('payt')?:'order';
        $Fruit = input('typeid');
        $user_id = $this->user_id;
        $paypwd = input('paypwd');
//        if($this->user['paypwd']){
//            if (encrypt($paypwd) != $this->user['paypwd']){
//                return [
//                    'status' => 0,
//                    'msg' => '支付密码错误'
//                ];
//            }
//        }else{
//            Db::name('users')->where('user_id',$user_id)->update(['paypwd'=>encrypt($paypwd)]);
//        }
        writeLog('进入'.json_encode(input()),'pay');
        //支付
        Db::startTrans();
        $rs = logic('Pay')->pay($table,$ids,$this->user_id);
        writeLog(json_encode($rs),'pay');
        if(!$rs['status']){
            Db::rollback();
            return [
                'status' => 0,
                'msg' => $rs['msg']??'未知错误'
            ];
        }
        if($rs['status']==1){
            Db::commit();
            return [
                'status' => 1,
                'msg' => $rs['msg'],
                'to_url' => '/pages/myorder/orderlist?st=1'
            ];
        }

        Db::commit();
        //$Fruit = 'weixin_app';
        $platform = input('platform');
//        $platform = 'app';
        //在线支付方式
        if ($rs['status']==2 && $Fruit=='weixin'){
            //微信小程序支付
            $response = logic('Pay')->wxapp_pay($rs['id'],$platform);
            if($response['status']==1){
                return ['status'=>2,'data'=>$response['data']];
            }
        }
        if ($rs['status']==2 && $Fruit=='ali'){
            //支付宝支付
            $response = logic('Pay')->ali_pay($rs['id'],$platform);
            if($response['status']==1){
                return ['status'=>2,'data'=>$response['data']];
            }else{
                return $response;
            }
        }


        $result = [
            'status' => 0,
            'msg' => '支付失败，余额不足！',
            'data' => []
        ];
        return $result;
    }

}
